package io.renren.modules.work.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import io.renren.common.utils.ConfigConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.work.entity.ScGoodsEntity;
import io.renren.modules.work.service.ScGoodsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 器材
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-03-31 22:42:52
 */
@RestController
@RequestMapping("work/scgoods")
public class ScGoodsController {
    @Autowired
    private ScGoodsService scGoodsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("work:scgoods:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scGoodsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{goodsid}")
    @RequiresPermissions("work:scgoods:info")
    public R info(@PathVariable("goodsid") Integer goodsid){
		ScGoodsEntity scGoods = scGoodsService.getById(goodsid);

        return R.ok().put("scGoods", scGoods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("work:scgoods:save")
    public R save(@RequestBody ScGoodsEntity scGoods){
        scGoods.setGoodsno(ConfigConstant.generateGoodsNo());
		scGoodsService.save(scGoods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("work:scgoods:update")
    public R update(@RequestBody ScGoodsEntity scGoods){
		scGoodsService.updateById(scGoods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("work:scgoods:delete")
    public R delete(@RequestBody Integer[] goodsids){
		scGoodsService.removeByIds(Arrays.asList(goodsids));

        return R.ok();
    }


    /**
     * 获取商品列表
     */
    @RequestMapping("/getGoodsList")
    public R getGoodsList(){
        List<ScGoodsEntity> list = scGoodsService.list();
        return R.ok().put("list",list);
    }

    /**
     * 导入器材excel
     */
    @PostMapping("/importGoods")
    public R importGoods(MultipartFile file) throws IOException {
        //获取文件名
        String fileName = file.getOriginalFilename();
        //获取文件类型
        String fileType = "";
        if (fileName != null) {
            fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        if ("xlsx".equals(fileType) || "xls".equals(fileType)) {
            return scGoodsService.importExcel(file);
        }else{
            return R.error("非Excel类型");
        }

    }
}
